﻿# -*- coding: utf-8 -*-

import sys
from Ui_MainWindow import *
from Connection import *
from PyQt4.QtGui import QMessageBox
from PyQt4.QtCore import QString

class MainWindow(Ui_MainWindow, QtGui.QMainWindow):
	"""Class that implements the main windows functions"""
	def __init__(self):
		"""Default construtor"""
		super(MainWindow, self).__init__()
		self.setupUi(self)
		self.connection = Connection(self)
		self.mode = 0  # 0 = client; 1 = server
		self.waitingRestart = False

		#Connect signals and slots
		self.connect(self.connectButton, QtCore.SIGNAL("clicked()"), self.connectButtonClicked)
		self.connect(self.listenButton, QtCore.SIGNAL("clicked()"), self.listenButtonClicked)
		self.connect(self.clientCancelButton, QtCore.SIGNAL("clicked()"), self.clientCancelButtonClicked)
		self.connect(self.serverCancelButton, QtCore.SIGNAL("clicked()"), self.serverCancelButtonClicked)
		self.connect(self.restartButton, QtCore.SIGNAL("clicked()"), self.restartButtonClicked)

	def connectButtonClicked(self):
		port,ok = self.clientPortText.text().toULong()
		if (ok):
			self.clientServerBox.setEnabled(False)
			self.connectingLabel.show()
			self.clientCancelButton.show()
			self.hostnameText.setEnabled(False)
			self.clientPortText.setEnabled(False)
			self.connectButton.hide()
			# conectar com o servidor
			self.connection.connect(self.hostnameText.text(), port)
			self.mode = 0
		else:
			QMessageBox(self).critical(self, "Erro", QString.fromUtf8("A porta deve ser um número inteiro!"),QMessageBox.Ok)
	
	def listenButtonClicked(self):
		self.clientServerBox.setEnabled(False)
		self.listeningLabel.show()
		self.serverCancelButton.show()
		self.listenButton.hide()
		self.connection.listen()
		self.listeningLabel.setText(QString("Esperando cliente na porta %1").arg(self.connection.getServerPort()))
		self.mode = 1
			
	def clientCancelButtonClicked(self):
		self.clientServerBox.setEnabled(True)
		self.connectingLabel.hide()
		self.clientCancelButton.hide()
		self.hostnameText.setEnabled(True)
		self.clientPortText.setEnabled(True)
		self.connectButton.show()
		self.connection.cancel()
	
	def serverCancelButtonClicked(self):
		self.clientServerBox.setEnabled(True)
		self.listeningLabel.hide()
		self.serverCancelButton.hide()
		self.listenButton.show()
		self.connection.cancel()

	def restartButtonClicked(self):
		self.connection.sendRestart()
		if (self.waitingRestart):
			self.waitingRestart = False
			self.winLoseWidget.hide()
			self.GlWidget.show()
			self.GlWidget.initializeGame()
		else:
			self.waitingRestart = True
			self.restartLabel.setText("Aguardando o outro jogador...")
			self.restartButton.setEnabled(False)
			self.exitButton.setEnabled(False)

	def restartGame(self):
		if (self.waitingRestart):
			self.waitingRestart = False
			self.winLoseWidget.hide()
			self.GlWidget.show()
			self.GlWidget.initializeGame()
			self.restartLabel.setText("Deseja iniciar uma nova partida?")
			self.restartButton.setEnabled(True)
			self.exitButton.setEnabled(True)
		else:
			self.waitingRestart = True

	def initializeGame(self):
		self.widget.hide()
		self.GlWidget.show()
		self.GlWidget.initializeGame()
			
if __name__ == '__main__':
	app = QtGui.QApplication(sys.argv)
	window = MainWindow()
	window.show()
	sys.exit(app.exec_())
